package jgl.vajra.kingkong.part.vehicle.pop;

import android.app.Activity;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;

import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.FragmentActivity;

import com.orhanobut.logger.Logger;

import java.lang.ref.WeakReference;

import jgl.vajra.kingkong.R;
import jgl.vajra.kingkong.databinding.PopFollowingCarBinding;
import jgl.vajra.kingkong.widget.utils.UIUtils;

public class FollowingCarPop {
    private WeakReference<FragmentActivity> activityWeakReference;
    private PopupWindow menuPop;
    private View mPopRootView;
    private PopFollowingCarBinding mBinding;
    private final Activity mContext;

    public FollowingCarPop(FragmentActivity activity) {
        mContext = activity;
        activityWeakReference = new WeakReference<>(activity);
    }

    private void initPopupWindow() {
        mPopRootView = View.inflate(UIUtils.getContext(), R.layout.pop_following_car, null);
        mBinding = DataBindingUtil.bind(mPopRootView);
        if (mBinding != null)
            mBinding.setView(this);

        menuPop = new PopupWindow(mPopRootView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        menuPop.setOutsideTouchable(false);
        menuPop.setBackgroundDrawable(new BitmapDrawable());
        menuPop.setFocusable(true);// 点击空白处时，隐藏掉pop窗口
        menuPop.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                Logger.d("------onDismiss");
                //popupwindow消失的时候恢复成原来的透明度
                // backgroundAlpha(R.color.gray_f8);
                backgroundAlpha(1f);

            }
        });
    }


    /**
     * 设置添加屏幕的背景透明度
     *
     * @param bgAlpha
     */
    public void backgroundAlpha(float bgAlpha) {
        if (activityWeakReference.get() == null) return;
        if (activityWeakReference != null) {
            WindowManager.LayoutParams lp = activityWeakReference.get().getWindow().getAttributes();
            lp.alpha = bgAlpha; //0.0-1.0
            activityWeakReference.get().getWindow().setAttributes(lp);
        }
    }

    public void showPopupWindow(View v) {
        if (menuPop == null)
            initPopupWindow();
        int[] location = new int[2];
        v.getLocationOnScreen(location);  //获取控件的位置坐标
        //获取自身的长宽高
        mPopRootView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        //backgroundAlpha(R.color.titleBackgroundColor);
        backgroundAlpha(0.8f);
        menuPop.showAtLocation(v, Gravity.BOTTOM, 0, 0);  //显示指定控件的上方
    }

    public boolean isShowing() {
        return menuPop != null && menuPop.isShowing();
    }

    public void dismiss() {
        if (isShowing()) {
            menuPop.dismiss();
        }
    }

    //确定
    public void onDetermine() {
    }


}

